
//
//  QZHairDetailViewController.m
//  SmallShoppingCenter
//
//  Created by 上官惠阳 on 15/10/14.
//  Copyright (c) 2015年 上官惠阳. All rights reserved.
//

#import "QZHairDetailViewController.h"
#import "QZBottomView.h"
#import "QZHairHeadCell.h"
#import "QZTextView.h"
#import "QZOederViewController.h"
#import "QZHairDetailModel.h"
#import "PersonInfoViewController.h"

@interface QZHairDetailViewController ()

@end

@implementation QZHairDetailViewController
{
    QZHairHeadCell *_headView;
    QZHairDetailModel *_detailModel;
    UIButton *_collectionBtn;//喜欢按钮
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        _detailModel = [[QZHairDetailModel alloc]init];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"发型详情";
    
    [self addBtnOnNavWithTitle:@"发型" andImageName:@"返回" andTarget:self andAction:@selector(backAction) andFrame:CGRectMake(0, 0, 45, 20) andDirection:left];
    
    [self createTableViewWithPlainStyle:YES andSeparatorStyleNone:NO andBackGroudImageName:nil];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.frame = CGRectMake(0, 0, WIDTH, HEIGHT-64);
    
    [self registCellWithNib:@"QZHairHeadCell" addIdentifier:@"QZHairHeadCell" addHeight:300];
    [self registCellWithNib:@"QZTextView" addIdentifier:@"QZTextView" addHeight:300];
    
    [self createFooterView];
    
    [self loadData];
    
    [self requestData];
    
}
-(void)requestData
{
    if (_userId) {
        [self requestWithKeys:@[@"hair_id",@"user_id"] andValues:@[@([_hairFormId intValue]),@([_userId intValue])] andUrlKey:@"Hair.GetHairInfo" isCache:YES];
    }

}

-(void)request_POST_FinishValue:(id)value tagNSString:(NSString *)tag
{

    if ([tag isEqualToString:@"Hair.GetHairInfo"]) {
        if ([value[@"data"][@"status"] intValue] == 100) {
        
            _detailModel = [[QZHairDetailModel alloc] initWithDictionary:value[@"data"][@"data"] error:nil];
            [self.tableView reloadData];
        }
    }else if ([tag isEqualToString:@"Hair.GetHairPeople"]){
        
        if ([value[@"data"][@"status"] intValue] == 100) {
            
            if (_collectionBtn.selected) {
                _detailModel.people = [NSString stringWithFormat:@"%d",[_detailModel.people intValue] - 1];
            }else{
                _detailModel.people = [NSString stringWithFormat:@"%d",[_detailModel.people intValue] + 1];
            }
            _detailModel.is_people = !_detailModel.is_people;
            [self.tableView reloadData];
        }else{
            [[iToast makeText:@"操作失败了"]show];
        }
    }
}
-(void)loadData
{
    [self.tableSource addObject:@[@""]];
    [self.tableSource addObject:@[@""]];
}
-(void)createFooterView
{
    QZBottomView *bottom = [[[NSBundle mainBundle]loadNibNamed:@"QZBottomView" owner:self options:nil]lastObject];
    [bottom.bottomBtn addTarget:self action:@selector(orderAction:) forControlEvents:UIControlEventTouchUpInside];
    bottom.frame = CGRectMake(0, HEIGHT-48-64, WIDTH, 49);
    [self.view addSubview:bottom];
}
#pragma mark- 预约按钮
-(void)orderAction:(UIButton *)btn
{
    QZOederViewController *orderVc = [[QZOederViewController alloc]init];
    orderVc.hairFormId = _hairFormId;
    orderVc.user_id = _detailModel.user_id;
    orderVc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:orderVc animated:YES];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        QZHairHeadCell *head = [tableView dequeueReusableCellWithIdentifier:@"QZHairHeadCell"];
        if ([_detailModel.img_list isKindOfClass:[NSArray class]]) {
            //添加图片数组
           [head addImageViewToScrollWithArr:_detailModel.img_list];
        }
        head.describeLabel.textColor = OneOTwoColor;
        head.describeLabel.text = _detailModel.hairName;
        
        //点击设计师跳到设计师主页
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapDesignerAction)];
        [head.describeLabel addGestureRecognizer:tap];
        
        head.numLabel.textColor = OneFiveThreeColor;
        head.numLabel.text = _detailModel.people;

        head.collectionBtn.selected = _detailModel.is_people;
//        head.collectionBtn.selected = NO;
        [head.collectionBtn addTarget:self action:@selector(collectionAction:) forControlEvents:UIControlEventTouchUpInside];
        _collectionBtn = head.collectionBtn;
        
        head.selectionStyle = UITableViewCellSelectionStyleNone;
        return head;
    }
    if (indexPath.row == 1) {
        QZTextView *text = [tableView dequeueReusableCellWithIdentifier:@"QZTextView"];
        text.contentView.backgroundColor = BACKGROUDCOLOR;
        text.textView.text = _detailModel.hairFormDepict;
        text.selectionStyle = UITableViewCellSelectionStyleNone;
        return text;
    }
    return nil;
}
#pragma mark- 设计师资料
-(void)tapDesignerAction
{
    PersonInfoViewController *personVc = [[PersonInfoViewController alloc]init];
    personVc.designer_ID = _detailModel.user_id;
    [self.navigationController pushViewController:personVc animated:YES];
}
#pragma mark- 喜欢点击事件
-(void)collectionAction:(UIButton *)btn
{
    [self requestWithKeys:@[@"hair_id",@"user_id"] andValues:@[@([_hairFormId intValue]),@([_userId intValue])] andUrlKey:@"Hair.GetHairPeople" isCache:NO];
}
-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
